package com.pro.controller;

import com.alibaba.fastjson.JSONObject;
import com.pro.domain.Record;
import com.pro.domain.User;
import com.pro.service.UserService;
import com.pro.service.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * User login
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/userLogin")
    public Map<String, Object> userLogin(String username, String password) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            Map<String, Object> userLogin = userService.userLogin(username, password);
            map.put("token", userLogin.get("token"));
            map.put("data", userLogin.get("user"));
            map.put("code", "0");
            map.put("msg", "登录成功");
        } catch (ServiceException e) {
            e.printStackTrace();
            map.put("code", e.getCode());
            map.put("msg", e.getMsg());
        }
        return map;
    }

    /**
     * Get user's information from Redis by user id in token
     * @param token  -- user's information cached in Redis
     * @return
     */
    @GetMapping("/queryUserByUserId")
    public User queryUserByUserId(@RequestHeader(name = "token") String token) {
        return userService.queryUserByRedis(token);
    }

    /**
     * Get a user's records of purchasing tickets from Redis
     * @param token
     * @return
     */
    @GetMapping("/queryUserSelfRecord")
    public List<Record> queryUserSelfRecord(@RequestHeader(name = "token") String token) {
        return userService.queryUserSelfRecord(token);
    }

    /**
     * A user purchases one or more tickets
     * @param token
     * @param record1
     * @param num  -- id of a seat in theater room
     * @return
     */
    @PostMapping("/BuyTickets")
    public Map<String, Object> BuyTickets(@RequestHeader(name = "token") String token, Record record1, String[] num) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            userService.BuyTickets(token, record1, num);
            map.put("code", "0");
            map.put("msg", "购票成功");
        } catch (ServiceException e) {
            e.printStackTrace();
            map.put("code", e.getCode());
            map.put("msg", e.getMessage());
        }
        return map;
    }

    /**
     * 测试
     * @param token
     * @return
     */
    @GetMapping("/loginThen")
    public Object loginTest(@RequestHeader("token") String token) {
        if (token != null) {
            String s = redisTemplate.boundValueOps(token).get();
            return JSONObject.parseObject(s, User.class);
        }
        return "no login";
    }

}
